package constant.milk.cashbook.util;

import java.util.ArrayList;

import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import constant.milk.cashbook.insert.InsertNormalActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

public class TimeActivity extends SuperActivity
{
	public static final String TIME_AM_PM_DATA = "TIME_AM_PM_DATA";
	public static final String TIME_HOUR_DATA = "TIME_HOUR_DATA";
	public static final String TIME_MINUTE_DATA = "TIME_MINUTE_DATA";
	
	private View amView;
	private View pmView;
	
	private TextView hour1TextView;
	private TextView hour2TextView;
	private TextView minute1TextView;
	private TextView minute2TextView;

	private View hour1UpImageButton;
	private View hour1DownImageButton;
	private View hour2UpImageButton;
	private View hour2DownImageButton;
	private View minute1UpImageButton;
	private View minute1DownImageButton;
	private View minute2UpImageButton;
	private View minute2DownImageButton;
	
	private Button checkButton;
	
	private String am_pm;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.time_activity);
		
		setTitle("시간선택");
		
		setGoneTipButton();
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		amView = findViewById(R.id.timeAmView);
		pmView = findViewById(R.id.timePmView);
		
		hour1TextView = (TextView) findViewById(R.id.timeHour1TextView);
		hour2TextView = (TextView) findViewById(R.id.timeHour2TextView);
		minute1TextView = (TextView) findViewById(R.id.timeMinute1TextView);
		minute2TextView = (TextView) findViewById(R.id.timeMinute2TextView);

		hour1UpImageButton = findViewById(R.id.timeHourUp1ImageButton);
		hour1DownImageButton = findViewById(R.id.timeHourDown1ImageButton);
		
		hour2UpImageButton = findViewById(R.id.timeHourUp2ImageButton);
		hour2DownImageButton = findViewById(R.id.timeHourDown2ImageButton);
		
		minute1UpImageButton = findViewById(R.id.timeMinuteUp1ImageButton);
		minute1DownImageButton = findViewById(R.id.timeMinuteDown1ImageButton);
		
		minute2UpImageButton = findViewById(R.id.timeMinuteUp2ImageButton);
		minute2DownImageButton = findViewById(R.id.timeMinuteDown2ImageButton);
		
		checkButton = (Button) findViewById(R.id.checkButton);
	}

	@Override
	protected void setOnClick()
	{
		amView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				int padding = MilkUtil.dipToPixel(superContext, 10);
				amView.setBackgroundResource(R.drawable.btn_neon_push);
				amView.setPadding(padding, padding, padding, padding);
				pmView.setBackgroundResource(0);
				pmView.setPadding(padding, padding, padding, padding);
				
				am_pm = "AM";
			}
		});
		
		pmView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				int padding = MilkUtil.dipToPixel(superContext, 10);
				amView.setBackgroundResource(0);
				amView.setPadding(padding, padding, padding, padding);
				pmView.setBackgroundResource(R.drawable.btn_neon_push);
				pmView.setPadding(padding, padding, padding, padding);
				
				am_pm = "PM";
			}
		});
		
		hour1UpImageButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInputHourUp1();
			}
		});
		
		hour1DownImageButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInputHourDown1();
			}
		});
		
		hour2UpImageButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInputHourUp2();
			}
		});
		
		hour2DownImageButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInputHourDown2();
			}
		});
		
		minute1UpImageButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInputMinuteUp1();
			}
		});
		
		minute1DownImageButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInputMinuteDown1();
			}
		});
		
		minute2UpImageButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInputMinuteUp2();
			}
		});
		
		minute2DownImageButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setTextInputMinuteDown2();
			}
		});
		
		checkButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				String hour = hour1TextView.getText().toString() + hour2TextView.getText().toString();
				String minute = minute1TextView.getText().toString() + 
						minute2TextView.getText().toString();
				
				Intent intent = new Intent();
				intent.putExtra(TIME_AM_PM_DATA, am_pm);
				intent.putExtra(TIME_HOUR_DATA, hour);
				intent.putExtra(TIME_MINUTE_DATA, minute);
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
		});
	}

	@Override
	protected void setData()
	{
		Intent intent = getIntent();
		am_pm = intent.getStringExtra(TIME_AM_PM_DATA);
		
		String hour = intent.getStringExtra(TIME_HOUR_DATA);
		String minute = intent.getStringExtra(TIME_MINUTE_DATA);
		
		String hour1 = "";
		String hour2 = "";
		String minute1 = "";
		String minute2 = "";
		try
		{
			hour1 = hour.substring(0, 1);
			hour2 = hour.substring(1, 2);
			
			minute1 = minute.substring(0, 1);
			minute2 = minute.substring(1, 2);
		} catch (Exception e)
		{
			e.printStackTrace();
			MilkUtil.showToast(superContext, "시간 정보를 가져오는데에 실패 하였습니다.");
		}
		
		if (am_pm.equals("AM"))
		{
			amView.setBackgroundResource(R.drawable.btn_neon_push);
			pmView.setBackgroundResource(0);
		}
		else
		{
			amView.setBackgroundResource(0);
			pmView.setBackgroundResource(R.drawable.btn_neon_push);
		}
		
		hour1TextView.setText(hour1);
		hour2TextView.setText(hour2);
		minute1TextView.setText(minute1);
		minute2TextView.setText(minute2);
		
		hour1TextView.setTextSize(25);
		hour2TextView.setTextSize(25);
		minute1TextView.setTextSize(25);
		minute2TextView.setTextSize(25);
	}

	private void setTextInputHourUp1()
	{
		String strData = hour1TextView.getText().toString();
		
		int intData = Integer.parseInt(strData);
		
		if (intData == 1)
		{
			hour1TextView.setText("0");
		}
		else
		{
			hour1TextView.setText("1");
			hour2TextView.setText("0");
		}
	}
	
	private void setTextInputHourUp2()
	{
		String strData1 = hour1TextView.getText().toString();
		String strData2 = hour2TextView.getText().toString();
		
		int intData1 = Integer.parseInt(strData1);
		int intData2 = Integer.parseInt(strData2);
		
		if (intData1 == 1)
		{
			if (intData2 == 2)
			{
				hour2TextView.setText("0");
			}
			else
			{
				intData2++;
				hour2TextView.setText(intData2 + "");
			}
		}
		else
		{
			if (intData2 == 9)
			{
				hour2TextView.setText("1");
			}
			else
			{
				intData2++;
				hour2TextView.setText(intData2 + "");
			}
		}
	}
	
	private void setTextInputHourDown1()
	{
		String strData = hour1TextView.getText().toString();
		
		int intData = Integer.parseInt(strData);
		
		if (intData == 1)
		{
			hour1TextView.setText("0");
		}
		else
		{
			hour1TextView.setText("1");
			hour2TextView.setText("0");
		}
	}
	
	private void setTextInputHourDown2()
	{
		String strData1 = hour1TextView.getText().toString();
		String strData2 = hour2TextView.getText().toString();
		
		int intData1 = Integer.parseInt(strData1);
		int intData2 = Integer.parseInt(strData2);
		
		if (intData1 == 1)
		{
			if (intData2 == 0)
			{
				hour2TextView.setText("2");
			}
			else
			{
				intData2--;
				hour2TextView.setText(intData2 + "");
			}
		}
		else
		{
			if (intData2 == 1)
			{
				hour2TextView.setText("9");
			}
			else
			{
				intData2--;
				hour2TextView.setText(intData2 + "");
			}
		}
	}
	
	private void setTextInputMinuteUp1()
	{
		String strData = minute1TextView.getText().toString();
		
		int intData = Integer.parseInt(strData);
		
		if (intData == 5)
		{
			minute1TextView.setText("0");
		}
		else
		{
			intData++;
			minute1TextView.setText(intData + "");
		}
	}
	
	private void setTextInputMinuteUp2()
	{
		String strData2 = minute2TextView.getText().toString();
		
		int intData2 = Integer.parseInt(strData2);
		
		if (intData2 == 9)
		{
			minute2TextView.setText("0");
		}
		else
		{
			intData2++;
			minute2TextView.setText(intData2 + "");
		}
	}
	
	private void setTextInputMinuteDown1()
	{
		String strData = minute1TextView.getText().toString();
		
		int intData = Integer.parseInt(strData);
		
		if (intData == 0)
		{
			minute1TextView.setText("5");
		}
		else
		{
			intData--;
			minute1TextView.setText(intData + "");
		}
	}
	
	private void setTextInputMinuteDown2()
	{
		String strData2 = minute2TextView.getText().toString();
		
		int intData2 = Integer.parseInt(strData2);
		
		if (intData2 == 0)
		{
			minute2TextView.setText("9");
		}
		else
		{
			intData2--;
			minute2TextView.setText(intData2 + "");
		}
	}
	
	@Override
	protected void tipButton()
	{
	}

}
